<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app {
            width: 50px;
            height: 50px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 0;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div id="app"></div>
    <script>
        const app = document.getElementById('app')
        let isPointerDown = false

        app.addEventListener('mousedown', function (e) {
            isPointerDown = true
        });

        document.addEventListener('mousemove', function (e) {
            app.setPointerCapture(e.pointerId)
            if (isPointerDown) {
                const left = app.getBoundingClientRect().left
                const top = app.getBoundingClientRect().top
                let newLeft = e.clientX - left
                let newTop = e.clientY - top
                console.log(newLeft, newTop);
                app.style.left = newLeft + left - app.clientWidth / 2 + 'px'
                app.style.top = newTop + top - app.clientHeight / 2 + 'px'
            }
        });

        document.addEventListener('mouseup', function (e) {
            isPointerDown = false
        });
    </script>
</body>

</html>